import config from "../../config.js";
var util = require('../../utils/util.js');
const openIdUrl = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateStart: util.getDate(),
    dateEnd: util.addYear(1),
    member: {},
    card: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var membercard = wx.getStorageSync("membercard");
    if (membercard != null || membercard != "") {

      this.setData({
        card: membercard
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  loadData: function() {
    var that = this
    wx.showLoading({
      title: '信息获取中...',
    })
    wx.request({
      url: openIdUrl.getSystemInfo,
      data: {},
      success: function(result) {
        console.log(result);
        wx.hideLoading();
        if (result.data.status == 200) {
          that.setData({
            member: result.data.data
          })
        }
      }
    })
  },

  //支付：
  payAction: function() {

    var that = this
    var price = 0.01;
    var openId = wx.getStorageSync("openID");
    //判断openId是否存在
    if(openId==null || openId==""){
      wx.login({
        success: function (res) {
          wx.request({
            url: openIdUrl.CheckOpenId,
            data: {
              code: res.code
            },
            success: function (res) {
              console.log("openId" + res.data.data.openid);
              wx.setStorageSync('openID', res.data.data.openid);
              // if (wx.getStorageSync("session") == null) {
              //   wx.setStorageSync("session", res.data.session);
              //   console.log("session:" + res.data.session);
              // }
              openId = res.data.data.openid;
              that.payOrder(openId);

            }
          })
        }
      })
    }else{
      that.payOrder(openId);
    }


    
  },
  payOrder: function (openId){
    var that = this;
    var orderId = that.data.orderId;
    console.log(orderId);
    wx.showLoading({
      title: '发起支付...',
    })
    wx.request({
      url: openIdUrl.ordePayMember,
      data: {
        //用户的openid
        openId: openId,
        price: that.data.card.price
      },
      success: function (result) {
        console.log(result);
        wx.hideLoading();
        if (result.data.status == 200) {
          //out_trade_no=res.data['out_trade_no'];
          wx.requestPayment({
            timeStamp: result.data.timeStamp,
            nonceStr: result.data.nonceStr,
            package: result.data.package,
            signType: 'MD5',
            paySign: result.data.paySign,
            success: function (successret) {
              console.log('支付成功');
              //获取支付用户的信息
              wx.showLoading({
                title: '支付成功！',
              })
              setTimeout(function () {
                that.payResult(result.data.orderNo, that.data.card.price);
                wx.hideLoading();
              }, 300);

            },
            fail: function (res) {
              console.log(res);

              wx.showToast({
                title: '支付取消！',
                icon: 'none',
                duration: 1000
              })

            }
          })
        } else {
          console.log();
        }
      }
    })
  },
  payResult: function(No, price) {
    var that = this;
    wx.showLoading({
      title: '更新订单...',
    })
    wx.request({
      url: openIdUrl.bindPayCard,
      data: {

        orderNo: No,
        price: price
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(result) {
        console.log(result);
        wx.hideLoading();
        if (result.data.status == 200) {
          wx.showLoading({
            title: '会员支付成功',
          })
          // that.loadData();
          setTimeout(function() {
            // wx.hideLoading();
            wx.navigateTo({
              url: './success',
            })

          }, 1000);

        } else if (result.data.status == 201) {
          //订单更新失败；
          //记录当前订单OrderId;
          // wx.setStorageSync("payError", id);
        }
      }
    })
  },
  agreementDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    var url = config.http + "://" + config.host + "/agreemmentContent.action?contentId=" + id;
    wx.setStorageSync("webviewUrl", url);
    wx.navigateTo({
      url: "../webview/webview"
    })
  }
})